package com.kuang.contrpller;

import com.kuang.dao.DepartmentDao;
import com.kuang.dao.EmployeeDao;
import com.kuang.pojo.Department;
import com.kuang.pojo.Employee;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Collection;

@Controller
public class employeeController {
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private DepartmentDao departmentDao;
    @RequestMapping("/emps")
    public String list(Model model){
        Collection<Employee> employees = employeeDao.selectEmployee();
        model.addAttribute("emps",employees);
        return "emp/list";
    }

    @RequestMapping("/emp")
    public String addUser(Model model){
        Collection<Department> dep = departmentDao.getDepartments();
        model.addAttribute("deps",dep);
        return "emp/add";
    }
    @PostMapping("/emp")
    public String add(Employee employee){
        employeeDao.addEmployee(employee);
        return "redirect:/emps";
    }
    @RequestMapping("/emp/{id}")
    public String update(@PathVariable("id") int id,Model model){
        Employee emplo = employeeDao.selectEmployeeId(id);
        model.addAttribute("emp",emplo);
        Collection<Department> dep = departmentDao.getDepartments();
        model.addAttribute("deps",dep);
        return "emp/update";
    }
    @PostMapping("/updateEmp")
    public String update(Employee employee){
        employeeDao.addEmployee(employee);
        return "redirect:/emps";
    }

    @RequestMapping("/delemp/{id}")
    public String delete(@PathVariable("id") int id , Model model){
        employeeDao.deleteEmployee(id);
        return "redirect:/emps";
    }
    @RequestMapping("/user/out")
    public String outuser(HttpSession session){
        session.removeAttribute("userlogin");
        return "redirect:/";
    }

}
